package web.rssReader;

import java.net.URL;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import Error.Error;

import data.Show;

/**
 * Interface for RSS Readers.
 * @author Quinn
 *
 */
public abstract class RssReaderInterface 
{
	/**
	 * The number of description values given.
	 */
	protected static int VALUE = 5;
	/**
	 * the time out tries to connect
	 */
	public static int TIMEOUT = 3;
	/**
	 * The URL to use for searches.
	 */
	protected URL url;
	protected String stringURL;
	protected String showName;

	public String getStringURL()
	{
		return this.stringURL;
	}
	public String getAttributeValue(Element e, String label, String attribute)
	{
		return ((Element)e.getElementsByTagName(label).item(0)).getAttribute(attribute);
	}
	public String getElementValue(Element parent,String label) 
	{
		return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));
	}
	public String getCharacterDataFromElement(Element e) 
	{
		try 
		{
			Node child = e.getFirstChild();
			if(child instanceof CharacterData) 
			{
				CharacterData cd = (CharacterData) child;
				return cd.getData();
			}
		}
		catch(Exception ex) 
		{}
		return "";
	}
	
	/**
	 * connects to the url, then add's it as a class variable.
	 * @param url String representation of the url to connect to.
	 */
	public void connect()
	{
		int i=0;
		boolean needURL = true;
		while(i < TIMEOUT && needURL)
		{
			try
			{
				this.url = new URL(this.getStringURL());
				needURL = false;
			}
			catch(Exception e)
			{
				if(i < TIMEOUT)
				{
					i++;
				}
				else
				{
					Error.report(20, this.getStringURL());
				}
			}
		}
	}
	

	/**
	 * Gets the shows from the RSS Feed and returns them in a LinkedList<Show>
	 * @return Shows in a LinkedList.
	 */
	public Show getShow()
	{
		Show show = new Show(this.showName);
		return show;
	}
}
